package web.servlet;

import domain.Account;
import domain.Order;
import service.LogService;
import service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class OrderListServlet extends HttpServlet {
    private static final String ORDER_LIST ="/WEB-INF/jsp/order/orderList.jsp";

    private String username;
    private OrderService orderService;
    private List<Order> orderList=new ArrayList<>();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        username=req.getParameter("username");
        orderService=new OrderService();
        orderList=orderService.getOrderByUsername(username);

        HttpSession session=req.getSession();
        session.setAttribute("orderList",orderList);

        Account account=(Account)session.getAttribute("account");
        if(account != null){
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + req.getContextPath() + req.getServletPath() + "?" + (req.getQueryString());

            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + strBackUrl + " 查看订单 " + orderList;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }

        req.getRequestDispatcher(ORDER_LIST).forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
